package com.sirdc.modules.sys.web.HardWork;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.EarlyWarningNode;
import net.huashitong.Project.filter.EarlyWarningNodeFilter;
import net.huashitong.Project.service.EarlyWarningNodeService;
import net.huashitong.node.filter.NodeNewschedulesFilter;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.oversee.filter.CoordinateFilter;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.CoordinateService;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.entity.SuperviseUser;
import net.huashitong.user.service.SuperviseUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyp1202 on 2018/7/31.
 */
@Controller
@RequestMapping("/warningManger")
public class SysWarningMangerController {
    @Autowired
    private EarlyWarningNodeService earlyWarningNodeService;
    @Autowired
    private SuperviseUserService superviseUserService;
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;
    @Autowired
    private SupervisionTaskService supervisionTaskService;
    @Autowired
    private CoordinateService coordinateService;

    @RequestMapping("/go/list")
    public String goWarning(Model model, HttpServletRequest request) {
        model.addAttribute("leftType", "逾期列表");
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if (ObjectUtils.isBlank(bnpUser)) {
            return "/modules/login/login";
        }
        EarlyWarningNodeFilter filter = new EarlyWarningNodeFilter();
        if (!"2".equals(bnpUser.getBranchType())) {
            filter.setOperatorUser(bnpUser.getName());
        }

        model.addAttribute("warning", earlyWarningNodeService.queryEarlyWarning(filter));
        model.addAttribute("totalPage", earlyWarningNodeService.querywarningCount(filter));

        int myTaskNum = (int) WebUtils.getSessionAttribute(request, "myTaskNum");
        int quetionNum = (int) WebUtils.getSessionAttribute(request, "quetionNum");
        model.addAttribute("myTaskNum", myTaskNum);
        model.addAttribute("quetionNum", quetionNum);

        int myTackTaskNum = (int) WebUtils.getSessionAttribute(request, "myTackTaskNum");
        model.addAttribute("myTackTaskNum", myTackTaskNum);

        //待办任务个数
        int nodeCount = (int) WebUtils.getSessionAttribute(request, "nodeCount");
        model.addAttribute("nodeCount", nodeCount);

        return "/modules/hardWork/user_warning";
    }

    @ResponseBody
    @RequestMapping("/go/lists")
    public Object warningInfo(EarlyWarningNodeFilter filter, HttpServletRequest request) {
        BnpUser bu = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if (ObjectUtils.isBlank(bu)) {
            return "/modules/login/login";
        }
        if(!"4".equals(bu.getBranchType())) {
            if (!"2".equals(bu.getBranchType())) {
                filter.setOperatorUser(bu.getName());
            }
            List<EarlyWarningNode> list = earlyWarningNodeService.queryEarlyWarning(filter);
            double totalPage=earlyWarningNodeService.querywarningCount(filter);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", list);
            map.put("totalPage", totalPage);
            return map;
        }else{
            List<SuperviseUser> suList= superviseUserService.querySuperviseUserListByPhone(bu.getPhone());
            String projectIds="";
            int s=1;
            List<EarlyWarningNode> list =null;
            double totalPage=0.0;
            if(suList.size()>0) {
                for (SuperviseUser su : suList) {
                    if (s == 1) {
                        projectIds = "'" + su.getProjectId() + "'";
                    } else {
                        projectIds = "'" + su.getProjectId() + "'," + projectIds;
                    }
                    s++;
                }
                filter.setProjectIds(projectIds);
                list = earlyWarningNodeService.queryEarlyWarning(filter);
                totalPage=earlyWarningNodeService.querywarningCount(filter);
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", list);
            map.put("totalPage", totalPage);
            return map;
         }
        }


    @RequestMapping(value ="/updateEarlyWarning",method = RequestMethod.POST)
    public ModelAndView updateEarlyWarning(@ModelAttribute EarlyWarningNodeFilter filter, Model model, HttpServletResponse response, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        EarlyWarningNode ew = earlyWarningNodeService.getById(filter.getSysId());
        BnpUser bu = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if (ObjectUtils.isBlank(bu)) {
            return new ModelAndView("redirect:/modules/login/login");
        }
        System.out.println("sdfsdf="+bu.getBranchType());
        if ("2".equals(bu.getBranchType())) {
            ew.setState("1");
            ew.setOpinions(filter.getOpinions());
        } else {
            ew.setContent(filter.getContent());
        }
        earlyWarningNodeService.update(ew);

        return new ModelAndView("redirect:/warningManger/go/list");
    }

    @ResponseBody
    @RequestMapping(value = "/queryEarlyWarning", method = RequestMethod.POST)
    public Object queryEarlyWarning(@RequestBody EarlyWarningNodeFilter filter) {
        EarlyWarningNode ew = earlyWarningNodeService.getById(filter.getSysId());
        return ew;
    }

}
